package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;

public enum UserInterfaceState {
    WELCOME {
        @Override
        public UserInterfaceState execute(Library library, PrintStream printStream, BufferedReader bufferedReader) {
            printStream.println("Welcome to Biblioteca - your friendly library management system");
            return LOGIN_PROMPT;
        }
    },
    LOGIN_PROMPT {
        @Override
        public UserInterfaceState execute(Library library, PrintStream printStream, BufferedReader bufferedReader) {
            printStream.print("Please enter your library number and press enter: ");
            String libraryNumber = readLineFromConsole(bufferedReader);
            printStream.print("Please enter your password and press enter: ");
            String password = readLineFromConsole(bufferedReader);
            if(library.loginUser(libraryNumber, password)){
                printStream.println("Valid credentials supplied, you are now logged in.");
                return MAIN_MENU;
            }
            printStream.println("Invalid credentials, log in failed!");
            return LOGIN_PROMPT;
        }
    },
    MAIN_MENU {
        @Override
        public UserInterfaceState execute(Library library, PrintStream printStream, BufferedReader bufferedReader) {
            printStream.print("-List Books\n-List Movies\n-Checkout Item\n-Return Item\n-Account Info\n-Quit\nPlease enter operation of choice: ");
            String input = readLineFromConsole(bufferedReader);
            try {
                return UserInterfaceState.valueOf(input.replaceAll(" ", "_").toUpperCase());
            } catch (IllegalArgumentException | NullPointerException ex) {
                return INCORRECT_MENU_OPTION;
            }
        }
    },
    CHECKOUT_ITEM {
        @Override
        public UserInterfaceState execute(Library library, PrintStream printStream, BufferedReader bufferedReader) {
            printStream.print("Enter the name of the item you wish to checkout and press enter: ");
            String input = readLineFromConsole(bufferedReader);
            printStream.println(library.checkoutItem(input));
            return MAIN_MENU;
        }
    },
    RETURN_ITEM {
        @Override
        public UserInterfaceState execute(Library library, PrintStream printStream, BufferedReader bufferedReader) {
            printStream.print("Enter the name of the item you wish to return and press enter: ");
            String input = readLineFromConsole(bufferedReader);
            printStream.println(library.returnItem(input));
            return MAIN_MENU;
        }
    },
    LIST_BOOKS {
        @Override
        public UserInterfaceState execute(Library library, PrintStream printStream, BufferedReader bufferedReader) {
            printListOfAvailableItemsByType(library, printStream, ItemType.BOOK);
            return MAIN_MENU;
        }
    },
    LIST_MOVIES {
        @Override
        public UserInterfaceState execute(Library library, PrintStream printStream, BufferedReader bufferedReader) {
            printListOfAvailableItemsByType(library, printStream, ItemType.MOVIE);
            return MAIN_MENU;
        }
    },
    ACCOUNT_INFO {
        @Override
        public UserInterfaceState execute(Library library, PrintStream printStream, BufferedReader bufferedReader) {
            printStream.println(library.getAccountInformation());
            return MAIN_MENU;
        }
    },
    QUIT {
        @Override
        public UserInterfaceState execute(Library library, PrintStream printStream, BufferedReader bufferedReader) {
            System.exit(0);
            return null;
        }
    },
    INCORRECT_MENU_OPTION {
        @Override
        public UserInterfaceState execute(Library library, PrintStream printStream, BufferedReader bufferedReader) {
            printStream.println("Select a valid option!");
            return MAIN_MENU;
        }
    };

    private static String readLineFromConsole(BufferedReader bufferedReader) {
        String input = "";
        try {
            input = bufferedReader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return input;
    }

    private static void printListOfAvailableItemsByType(Library library, PrintStream printStream, ItemType type) {
        for (Item item : library.getAvailableItemsOfType(type)) {
            printStream.println(item.toString());
        }
    }

    public abstract UserInterfaceState execute(Library library, PrintStream printStream, BufferedReader bufferedReader);
}